﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tosi
{
    class Car
    {
        private Point pos;
        private Point target;
        private Point tempPos;
        private Point temp2Pos;
        private Random rnd;
        private Street myStreet;
        private int id;
        private int initSpeed;
        private int tempSpeed;
        private void reposition()
        {
            Street dir = myStreet.getMyJunction().getDirection();
            if (dir != null)
            {
                if (dir.cars.Count < dir.getMaxCars())
                {
                    pos.setTo(dir.getStart());
                    target.setTo(dir.getEnd());
                    myStreet.carOut(this);
                    myStreet.myLight.reduceStationaryCars();
                    myStreet = dir;
                    myStreet.carIn(this);
                }
            }
            else tempSpeed = 0;
        }

        public Car(ref Street parent, int i)
        {
            myStreet = parent;
            pos = new Point(parent.getStart().getX(), parent.getStart().getY());
            target = new Point(parent.getEnd().getX(), parent.getEnd().getY());
            int Seed = (int)DateTime.Now.Ticks;
            rnd = new Random(Seed);
            initSpeed = rnd.Next(2, 6);
            tempSpeed = initSpeed;
            id = i;
            System.Threading.Thread.Sleep(10);
        }
        public void moveCar()
        {
            if (tempSpeed != 0)
            {
                tempPos = new Point(pos.getX() + tempSpeed * myStreet.getCosPhi(),
                                    pos.getY() + tempSpeed * myStreet.getSinPhi());
                if (myStreet.cars.IndexOf(this) != 0)
                {
                    Point lastPos = myStreet.cars[myStreet.cars.IndexOf(this) - 1].getCarPos();
                    if (tempSpeed < Var.CarOffset)
                        temp2Pos = new Point(pos.getX() + Var.CarOffset * myStreet.getCosPhi(),
                                             pos.getY() + Var.CarOffset * myStreet.getSinPhi());
                    else
                        temp2Pos.setTo(tempPos);
                    if ((pos < temp2Pos) * (temp2Pos < lastPos) == 1)
                    {
                        pos.setTo(tempPos);
                    }
                    else
                    {
                        pos.setX(lastPos.getX() - Var.CarOffset * myStreet.getCosPhi());
                        pos.setY(lastPos.getY() - Var.CarOffset * myStreet.getSinPhi());
                        tempSpeed = myStreet.cars[myStreet.cars.IndexOf(this) - 1].getTempSpeed();
                    }
                }
                else
                {
                    if (tempSpeed < Var.CarOffset)
                        temp2Pos = new Point(pos.getX() + Var.CarOffset * myStreet.getCosPhi(),
                                             pos.getY() + Var.CarOffset * myStreet.getSinPhi());
                    else temp2Pos.setTo(tempPos);
                    tempSpeed = initSpeed;
                    if ((pos < temp2Pos) * (temp2Pos < target) == 1)
                    {
                        pos.setTo(tempPos);
                    }
                    else
                    {
                        if (myStreet.myLight.isRed())
                        {
                            tempSpeed = 0;
                        }
                        else
                        {
                            tempSpeed = initSpeed;
                            reposition();
                        }
                    }

                }
            }
            else 
            {
                if (myStreet.cars.IndexOf(this) == 0)
                {
                    //if (myStreet.getMyLight().getState() == 0)
                    //{
                        tempSpeed = initSpeed;
                        moveCar();
                    //}
                }
                else tempSpeed = myStreet.cars[myStreet.cars.IndexOf(this) - 1].getTempSpeed();
            }
            Var.gr.setCar(id, pos);
        }
        public Point getCarPos()
        {
            return pos;
        }
        public int getId() {
            return id;
        }
        public int getTempSpeed() {
            return tempSpeed;
        }
    }
}
























